// test_comprehensive.cpp
#include "orthogonal_core.h"
#include "compatibility_bridge.h"
#include "profile_manager.h"
#include <iostream>
#include <cassert>
#include <vector>

using namespace cbConfig;

void TestOrthogonalCore() {
    std::cout << "=== Testing Orthogonal Core ===" << std::endl;
    
    // 测试基本配置创建
    BuildProfile profile;
    profile.name = "Test_Profile";
    profile.variant = BuildVariant::DEBUG;
    profile.os = OSFamily::WINDOWS;
    profile.architecture = ArchBits::BITS_64;
    profile.floatPrecision = FloatMode::DOUBLE;
    profile.compiler = Toolchain::MSVC;
    
    assert(profile.Validate());
    std::cout << "✓ Basic profile validation passed" << std::endl;
    
    // 测试编译器标志生成
    auto flags = profile.GetCompilerFlags();
    assert(!flags.empty());
    std::cout << "✓ Compiler flags generation: " << flags.size() << " flags" << std::endl;
    
    // 测试传统配置转换
    auto legacyProfile = BuildProfile::FromLegacy("Win32_Debug");
    assert(legacyProfile.Validate());
    std::cout << "✓ Legacy conversion: " << legacyProfile.ToLegacyName().ToStdString() << std::endl;
}

void TestCompatibilityBridge() {
    std::cout << "\n=== Testing Compatibility Bridge ===" << std::endl;
    
    auto& bridge = CompatibilityBridge::Get();
    
    // 测试格式检测
    wxString format = bridge.DetectProjectFormat("test.vcxproj");
    assert(format == "vcxproj");
    std::cout << "✓ Format detection: " << format.ToStdString() << std::endl;
    
    // 测试配置验证
    BuildProfile testProfile;
    testProfile.name = "Test";
    testProfile.variant = BuildVariant::DEBUG;
    testProfile.architecture = ArchBits::BITS_64;
    testProfile.SyncToLegacy();
    
    bool valid = bridge.ValidateCompatibility(testProfile);
    assert(valid);
    std::cout << "✓ Compatibility validation passed" << std::endl;
    
    // 测试配置修复
    BuildProfile brokenProfile = testProfile;
    brokenProfile.legacy.vcConfiguration = "Invalid";
    auto fixed = bridge.FixCompatibilityIssues(brokenProfile);
    assert(bridge.ValidateCompatibility(fixed));
    std::cout << "✓ Compatibility fixing passed" << std::endl;
}

void TestProfileManager() {
    std::cout << "\n=== Testing Profile Manager ===" << std::endl;
    
    auto& manager = ProfileManager::Get();
    
    // 测试预设配置
    auto debugProfile = manager.CreateDesktopDebug();
    auto releaseProfile = manager.CreateDesktopRelease();
    auto embeddedProfile = manager.CreateEmbeddedARM();
    
    assert(manager.CreateProfile(debugProfile));
    assert(manager.CreateProfile(releaseProfile));
    assert(manager.CreateProfile(embeddedProfile));
    std::cout << "✓ Preset profile creation passed" << std::endl;
    
    // 测试配置检索
    auto profiles = manager.GetAllProfiles();
    assert(profiles.size() >= 3);
    std::cout << "✓ Profile retrieval: " << profiles.size() << " profiles" << std::endl;
    
    // 测试配置推导
    auto derived = manager.DeriveProfile(debugProfile, BuildVariant::RELEASE);
    assert(derived.variant == BuildVariant::RELEASE);
    std::cout << "✓ Profile derivation passed" << std::endl;
    
    // 测试工具链检测
    auto toolchains = manager.GetAvailableToolchains();
    assert(!toolchains.empty());
    std::cout << "✓ Toolchain detection: " << toolchains.size() << " toolchains" << std::endl;
}

// 测试监听器
class TestListener : public ProfileManager::Listener {
public:
    void OnProfileAdded(const BuildProfile& profile) override {
        std::cout << "📝 Profile added: " << profile.name.ToStdString() << std::endl;
    }
    
    void OnProfileRemoved(const wxString& profileId) override {
        std::cout << "🗑️ Profile removed: " << profileId.ToStdString() << std::endl;
    }
    
    void OnProfileUpdated(const BuildProfile& profile) override {
        std::cout << "✏️ Profile updated: " << profile.name.ToStdString() << std::endl;
    }
};

void TestEvents() {
    std::cout << "\n=== Testing Events ===" << std::endl;
    
    auto& manager = ProfileManager::Get();
    TestListener listener;
    
    manager.AddListener(&listener);
    
    // 触发一些事件
    BuildProfile testProfile;
    testProfile.name = "Event_Test";
    testProfile.variant = BuildVariant::DEBUG;
    testProfile.os = OSFamily::LINUX;
    testProfile.architecture = ArchBits::BITS_64;
    testProfile.compiler = Toolchain::GCC;
    
    manager.CreateProfile(testProfile);
    manager.UpdateProfile(testProfile);
    manager.DeleteProfile(testProfile.GetId());
    
    manager.RemoveListener(&listener);
    std::cout << "✓ Event system test completed" << std::endl;
}

int main() {
    std::cout << "Starting Comprehensive Tests...\n" << std::endl;
    
    TestOrthogonalCore();
    TestCompatibilityBridge();
    TestProfileManager();
    TestEvents();
    
    std::cout << "\n🎉 All tests passed successfully!" << std::endl;
    return 0;
}